Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude dependencies of std for diagnostics #135278

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

tgross35
Copy link
Contributor

@tgross35 tgross35 commented Jan 9, 2025

Currently crates in the sysroot can show up in diagnostic suggestions, such as in #135232. To prevent this, do the following:

  • Mark crates that are dependencies of the standard library and nothing else as stdlib_private_dep
  • Introduce a query visible_crates that excludes these
  • Use the new .visible_crates(()) rather than .crates(()) for user-facing areas of code

This may be reviewed per-commit. This PR includes #135247, I'll rebase if that merges first.

Fixes: #135232

There are a few locations where the crate name is checked against an
enumerated list of `std`, `core`, `alloc`, and `proc_macro`, or some
subset thereof. In most of these cases, all four crates should likely be
treated the same. Change this so the crates are listed in one place, and
that list is used wherever a list of `std` crates is needed.

`test` could be considered relevant in some of these cases, but
generally treating it separate from the others seems preferable while it
is unstable.

There are also a few places that Clippy will be able to use this.
Currently `root` or `crate_root` is used to refer to an instance of
`CrateRoot` (representation of a crate's serialized metadata), but the
name `root` sometimes also refers to a `CratePath` representing a "root"
node in the dependency graph. In order to disambiguate, rename all
instances of the latter to `dep_root`.
Dependencies of `std`, `core` and others are members of the crate graph,
but should not be available for access by the local crate or show up in
user-facing diagnostics. To support restricting this, introduce a
`stdlib_private_dep` field on `CrateMetadata` that  contains this
information.
Add a query for crates that are visible and shoule appear in
diagnostics. This excludes stdlib-private crates unless the
`rustc_private` feature is set, in which case there is no difference
from the existing `.crates(())` query.
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
@rustbot
Copy link
Collaborator

rustbot commented Jan 9, 2025

r? @SparrowLii

rustbot has assigned @SparrowLii.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 9, 2025
@tgross35
Copy link
Contributor Author

tgross35 commented Jan 9, 2025

Unsure whether adding a query is okay or if this should just be a filtering method on tcx. I am still figuring out a good way to test this so

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 9, 2025
@tgross35
Copy link
Contributor Author

tgross35 commented Jan 9, 2025

Another option is to drop stdlib_private_dep and just use private_dep in the query. I didn't do this originally because I thought private_dep was more tightly coupled to the public_private_dependencies feature, but maybe this isn't the case.

Open to suggestions here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Diagnostics sometimes suggest items from rustc_private crates
3 participants